import 'package:flutter/material.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter_downloader/flutter_downloader.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:oral_cloud/config/app_config.dart';
import 'package:oral_cloud/config/constant.dart';
import 'package:oral_cloud/config/resource_mananger.dart';
import 'package:oral_cloud/model/medical_imaging.dart';
import 'package:oral_cloud/ui/widget/global_widget.dart';

import 'image.dart';
import 'my_linear_percent_indicator.dart';

class ImagingItemWidget extends StatelessWidget {
  final MedicalImaging imaging;
  final int index;
  final ImageItemTapCallback itemTap;
  ImagingItemWidget(
    this.imaging, {
    this.index,
    this.itemTap,
  });
  @override
  Widget build(BuildContext context) {
    return InkWell(
      onTap: () => itemTap(ImageItemTapType.tapTypeItem, index),
      child: Container(
          padding: EdgeInsets.symmetric(
              vertical: ScreenUtil.getInstance().setHeight(22)),
          child: Row(
            mainAxisSize: MainAxisSize.max,
            children: [
              RoundContainer(
                child: WrapperImage(
                  imageType: ImageType.normal,
                  url: imaging.thumb,
                  fit: BoxFit.cover,
                  height: ScreenUtil.getInstance().setHeight(190),
                  width: ScreenUtil.getInstance().setHeight(240),
                ),
              ),
              Expanded(
                child: Padding(
                  padding: EdgeInsets.symmetric(
                      horizontal: ScreenUtil.getInstance().setWidth(48),
                      vertical: 0),
                  child: Column(
                    mainAxisAlignment: MainAxisAlignment.spaceBetween,
                    crossAxisAlignment: CrossAxisAlignment.start,
                    children: [
                      Text(
                        imaging.name,
                        style: TextStyle(
                          color: Color(MYColors.textHighlightColor),
                          fontSize: FontSize.fontSizeL,
                        ),
                      ),
                      SizedBox(height: ScreenUtil.getInstance().setHeight(22)),
                      Row(
                        mainAxisAlignment: MainAxisAlignment.start,
                        children: [
                          NormalText('${imaging.count}帧'),
                          HSpaceWidget(),
                          NormalText(getTypeStr(imaging.type)),
                        ],
                      ),
                      SizedBox(height: ScreenUtil.getInstance().setHeight(36)),
                      DownloadWidget(
                        imaging,
                        onTap: itemTap,
                        index: index,
                      ),
                    ],
                  ),
                ),
              ),
              Image.asset(
                ImageHelper.wrapAssets('right_enter.png'),
                width: ScreenUtil.getInstance().setHeight(26),
                height: ScreenUtil.getInstance().setHeight(26),
              ),
            ],
          ),
          decoration: BoxDecoration(
            border: Border(
              bottom: BorderSide(
                width: 0.5,
                color: Color(0xffe5e5e5),
              ),
            ),
          )),
    );
  }

  String getTypeStr(int type) {
    switch (type) {
      case 1:
        return 'CT';
      case 2:
        return '局部CT';
      case 3:
        return '全景';
      case 4:
        return '侧位';
      case 5:
        return 'TMJ';
    }
    return "";
  }
}

// ignore: must_be_immutable
class ImagingItemWidgetOfPad extends StatelessWidget {
  final MedicalImaging imaging;
  final int index;
  final ImageItemTapCallback itemTap;

  bool isSelected;
  ImagingItemWidgetOfPad(this.imaging,
      {this.index, this.itemTap, this.isSelected: false});
  @override
  Widget build(BuildContext context) {
    return GestureDetector(
      onTap: () => itemTap(ImageItemTapType.tapTypeItem, index),
      child: Container(
        color: isSelected ? Color(0XFFEAECEE) : Colors.transparent,
        padding: EdgeInsets.symmetric(
            vertical: ScreenUtil.getInstance().setHeight(22)),
        child: Row(
          mainAxisSize: MainAxisSize.max,
          children: [
            RoundContainer(
              child: WrapperImage(
                imageType: ImageType.normal,
                url: imaging.thumb,
                height: ScreenUtil.getInstance().setHeight(142),
                width: ScreenUtil.getInstance().setHeight(176),
                fit: BoxFit.cover,
              ),
            ),
            Expanded(
              child: Padding(
                padding: EdgeInsets.symmetric(
                    horizontal: ScreenUtil.getInstance().setWidth(29)),
                child: Column(
                  mainAxisAlignment: MainAxisAlignment.spaceBetween,
                  crossAxisAlignment: CrossAxisAlignment.start,
                  children: [
                    Text(
                      imaging.name,
                      style: TextStyle(
                        color: Color(MYColors.textHighlightColor),
                        fontSize: FontSize.fontSizeL,
                      ),
                    ),
                    SizedBox(height: ScreenUtil.getInstance().setHeight(30)),
                    Row(
                      mainAxisAlignment: MainAxisAlignment.start,
                      children: [
                        NormalText('${imaging.count}帧'),
                        SizedBox(width: ScreenUtil.getInstance().setWidth(66)),
                        NormalText(getTypeStr(imaging.type)),
                      ],
                    ),
                    SizedBox(height: ScreenUtil.getInstance().setHeight(20)),
                    DownloadWidget(
                      imaging,
                      onTap: itemTap,
                      index: index,
                    ),
                  ],
                ),
              ),
            ),
          ],
        ),
      ),
    );
  }

  String getTypeStr(int type) {
    switch (type) {
      case 1:
        return 'CT';
      case 2:
        return '局部CT';
      case 3:
        return '全景';
      case 4:
        return '侧位';
      case 5:
        return 'TMJ';
    }
    return "";
  }
}

class SizeButton extends StatelessWidget {
  final double width;
  final double height;
  final VoidCallback onPressed;
  final Widget child;
  SizeButton({this.width, this.height, @required this.onPressed, this.child});
  @override
  Widget build(BuildContext context) {
    // TODO: implement build
    return Container(
      height: height,
      width: width,
      child: OutlineButton(
        onPressed: onPressed,
        child: child,
      ),
    );
  }
}

class DownloadWidget extends StatefulWidget {
  final ImageItemTapCallback onTap;
  final MedicalImaging imaging;
  final int index;
  DownloadWidget(
    this.imaging, {
    this.onTap,
    this.index,
  });
  @override
  State<StatefulWidget> createState() {
    // TODO: implement createState
    return _DownloadWidgetState();
  }
}



class _DownloadWidgetState extends State<DownloadWidget> {
  final double btnWidth =
      ScreenUtil.getInstance().setHeight(AppConfig.patientImgItemBtnWidth);
  final double btnHeight =
      ScreenUtil.getInstance().setHeight(AppConfig.patientImgItemBtnHeight);
  @override
  Widget build(BuildContext context) {
    if (widget.imaging.status == DownloadTaskStatus.running ||widget.imaging.status == DownloadTaskStatus.paused||
        widget.imaging.status == DownloadTaskStatus.enqueued) {
      print(
          '_DownloadWidgetState--->widget.imaging.progress=${widget.imaging.progress}');
      Widget progressWidget=MyLinearPercentIndicator(
        width: ScreenUtil.getInstance()
            .setWidth(AppConfig.patientImgItemProgressWidth),
        lineHeight: ScreenUtil.getInstance().setHeight(30),
        percent: widget.imaging.progress / 100,
        minProgress: 0.08,
        borderColor: Theme.of(context).primaryColor,
        progressColor: Theme.of(context).primaryColor,
        trailing: Padding(
          padding: EdgeInsets.only(left: ScreenUtil.getInstance().setWidth(8)),
          child: Text(
            '${widget.imaging.progress}%',
            style: TextStyle(
              color: Color(MYColors.textHighlightColor),
              fontSize: FontSize.fontSizeM,
            ),
          ),
        ),
      );
      return Container(
        alignment: Alignment.center,
        height: btnHeight,
        child: Row(
          crossAxisAlignment: CrossAxisAlignment.center,
          children: [
            progressWidget,
            SizedBox(width: 10,),
            GestureDetector(
              onTap: ()=>{
                widget.onTap(ImageItemTapType.tapTypeCancel, widget.index),
              },
              child: Text("取消",style: TextStyle(fontSize: FontSize.fontSizeXL),),

            ),
          ],
        ),
      );
    } else if (widget.imaging.status == DownloadTaskStatus.complete) {
      return Row(
        children: [
//          CommonBtnWidget('2D',width: btnWidth,height:btnHeight ,onTap:()=>_jumpTo2DPriview(widget.imaging.imageList)),
//          SizedBox(width:ScreenUtil.getInstance().setWidth(18) ,),
          CommonBtnWidget(
            '3D',
            width: btnWidth,
            height: btnHeight,
            onTap: () =>
                widget.onTap(ImageItemTapType.tapTypeJump, widget.index),
          ),
        ],
      );
    } else if(widget.imaging.type==1||widget.imaging.type==2){
      List<Widget> children = [
        CommonBtnWidget(
            widget.imaging.status == DownloadTaskStatus.failed ? '重试' : '下载',
            width: btnWidth,
            height: btnHeight,
            isLoading:widget.imaging.reqStatus==MedicalImageStatus.progressing ,
            onTap: () =>
                widget.onTap(widget.imaging.status == DownloadTaskStatus.failed?ImageItemTapType.tapTypeRetry:ImageItemTapType.tapTypeDownload, widget.index))
      ];
      if (widget.imaging.status == DownloadTaskStatus.failed) {
        children.add(Padding(
          padding: EdgeInsets.only(left: ScreenUtil.getInstance().setWidth(20)),
          child: NormalText('网络错误'),
        ));
      }
      return Row(
        children: children,
      );
    }
    else{
      return Container();
    }
  }
}

typedef ImageItemTapCallback = void Function(int tapType, int index);

class ImageItemTapType {
  static const int tapTypeItem = 0;
  static const int tapTypeDownload = 1;
  static const int tapTypeJump = 2;
  static const int tapTypePause = 3;
  static const int tapTypeResume = 4;
  static const int tapTypeCancel = 5;
  static const int tapTypeRetry = 6;
  static const int tapTypeReMove= 7;
}

class HSpaceWidget extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    // TODO: implement build
    return SizedBox(width: ScreenUtil.getInstance().setWidth(48));
  }
}
